/*
*部门
**DepartmentList.js*/

import React , {Component,PropTypes}from 'react';
import {connect} from 'dva';
import { helper } from '../../../../utils';
import {Tree} from 'antd';

import style from './departMentList.less';
const TreeNode = Tree.TreeNode;

class DepartmentList extends Component {
	
	
  componentWillMount() {
  	console.log('DepartmentList>this.props',this.props)
  	const selectArr = [];
  	const selectDataArr = [];
  	const selectCode = [];
  	if(this.props.selectData){
  		this.props.selectData.map((i)=>{
  			this.props.departmentData.map((k,j)=>{
  				if(i.name == k.name){
  					selectArr.push(this.props.departmentData[j])
  					selectCode.push(this.props.departmentData[j].code)
  				}
  			})
  		})
  		selectArr.map((item)=>{
	  		this.props.departmentData.map((k,j)=>{
					if(item.parentId == k.code){
						selectArr.push(this.props.departmentData[j])
					}
				})
	  	})
			for(let i = 0;i<selectArr.length;i++){
		    if(selectDataArr.indexOf(selectArr[i]) == -1){
		      selectDataArr.push(selectArr[i]);
		    }
			}
			console.log('DepartmentList>>',selectCode,selectArr,selectDataArr)
			this.state = {
		    autoExpandParent: true,
		    checkable:false,
		    expandedKeys:[] ,
		    checkedKeys: selectCode,
		    selectedKeys: [],
		    data:selectDataArr,
		    treeData:helper.arrayToTree(selectDataArr.map(item=>({
		    		title: item.name,
		    		key: item.code,
		    		id: item.code,
		    		pid: item.parentId,
		    	})))
		    
		  }
  	}else if(this.props.departmentCode){
  		this.props.dispatch({
  			type:`privateGame/setState`,
	      payload:{
	        'departmentIdList':this.props.departmentId
	      },
  		})
			this.state = {
		    autoExpandParent: true,
		    checkable:true,
		    expandedKeys:[] ,
		    checkedKeys: this.props.departmentCode,
		    selectedKeys: [],
		    data:this.props.departmentData,
		    treeData:[{
		    	title: '部门',
		    	key:'root',
		    	id: 'root',
		    	children:helper.arrayToTree(this.props.departmentData.map(item=>({
		    		title: item.name,
		    		key: item.code,
		    		id: item.code,
		    		pid: item.parentId,
		    	})))
		    }]
		  }
		}else{
			this.state = {
		    autoExpandParent: true,
		    checkable:true,
		    expandedKeys:[] ,
		    checkedKeys: [],
		    selectedKeys: [],
		    data:this.props.departmentData,
		    treeData:[{
		    	title: '部门',
		    	key:'root',
		    	id: 'root',
		    	children:helper.arrayToTree(this.props.departmentData.map(item=>({
		    		title: item.name,
		    		key: item.code,
		    		id: item.code,
		    		pid: item.parentId,
		    	})))
		    }]
		  }
		}
  }
  componentWillReceiveProps(nextProps){
		console.log('DepartmentList>nextProps',nextProps,this.props)
			const selectArr = [];
	  	const selectDataArr = [];
	  	const selectCode = [];
	  	const choiceId = []
	  	if(nextProps.selectData){
	  		nextProps.selectData.map((i)=>{
	  			nextProps.departmentData.map((k,j)=>{
	  				if(i.id == k.id){
	  					selectArr.push(nextProps.departmentData[j])
	  					selectCode.push(nextProps.departmentData[j].code)
	  				}
	  			})
	  		})
	  		selectArr.map((item)=>{
		  		nextProps.departmentData.map((k,j)=>{
						if(item.parentId == k.code){
							selectArr.push(nextProps.departmentData[j])
						}
					})
		  	})
				for(let i = 0;i<selectArr.length;i++){
			    if(selectDataArr.indexOf(selectArr[i]) == -1){
			      selectDataArr.push(selectArr[i]);
			    }
				}
				console.log('DepartmentList>>',selectCode,selectArr,selectDataArr)
				this.state = {
			    autoExpandParent: true,
			    checkable:false,
			    expandedKeys:[] ,
			    checkedKeys: selectCode,
			    selectedKeys: [],
			    data:selectDataArr,
			    treeData:helper.arrayToTree(selectDataArr.map(item=>({
		    		title: item.name,
		    		key: item.code,
		    		id: item.code,
		    		pid: item.parentId,
		    	})))
			    
			  }
	  	}
	  	
	}
  onExpand = (expandedKeys) => {

		this.setState({expandedKeys, autoExpandParent: false})
	}

	onCheck = (checkedKeys) => {
		console.log('onCheck>>',checkedKeys,this.props)
		
		const {departmentData,departmentIdList} = this.props;
		const a = [];
		checkedKeys.map((item,i)=>{
			if(item == 'root'){
				checkedKeys.splice(i, 1)
			}
		})
		this.setState({checkedKeys});
		const newCheckedKeys = [];
		const newCheckedArr = [];//选中的部门集合 
		checkedKeys.map((item)=>{
    	const node=this.state.data.find(iitem=>iitem.code===item)
    	newCheckedKeys.push(node)
    })
    newCheckedKeys.map((item,i)=>{
    	console.log('newCheckedKeys',item)
    	for(let k=0;k<departmentData.length;k++){
    		if(item.parentId == departmentData[k].code){
    			newCheckedArr.push(item)
    		}
    	}
    })
    newCheckedArr.map((i)=>{
    	a.push(i.id)
    })
    Array.from(new Set(departmentIdList))
    this.props.dispatch({
      type:`privateGame/setState`,
      payload:{
        'departmentIdList':a
      },
    })
    console.log('onCheck',newCheckedKeys,newCheckedArr)
		
	}
	onSelect = (selectedKeys) => {
		this.setState({selectedKeys})
	}
	renderTreeNodes = (data) => {
		return data.map((item,i) => {
			if (item.children) {
				const myProps={
					title:item.title,
					key:item.key,
					dataRef:item,
				}
				return (
					<TreeNode {...myProps}>
						{this.renderTreeNodes(item.children)}
					</TreeNode>
				)
			}
			return <TreeNode key={i} {...item}/>
		})
	}
	render(){
		const myProps={
			checkable:this.state.checkable,
			onExpand:this.onExpand,
			expandedKeys:this.state.expandedKeys,
			autoExpandParent:this.state.autoExpandParent,
			onCheck:this.onCheck,
			checkedKeys:this.state.checkedKeys,
			onSelect:this.onSelect,
			selectedKeys:this.state.selectedKeys,
		}
		return(
			<Tree {...myProps}>
        {this.renderTreeNodes(this.state.treeData)}
      </Tree>
		)
	}
}


export default connect(({privateGame}) =>({privateGame}))(DepartmentList)